package com.foreveross.proxyip.core.pools.memcache;

import java.util.HashSet;
import java.util.Set;

/*import net.rubyeye.xmemcached.MemcachedClient;

import org.apache.commons.lang3.StringUtils;
import org.mortbay.log.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.foreveross.proxyip.core.pools.DbPool;
import com.foreveross.proxyip.core.pools.PublicPool;
import com.foreveross.proxyip.entity.ProxyIpBean;
import com.foreveross.proxyip.infra.exception.CacheNotSupportException;
import com.foreveross.proxyip.infra.memcached.MemcachedManager;*/
/**
 * pool操作memcached的类；
 * realKey：保存memcached真实的键；
 * key：未加入字符串DEFALUT_CACHE_KEY_PRE，realKey的部分
 * @author lyq
 *
 */
//@Service
//public class PoolCacheManager extends MemcachedManager {
public class PoolCacheManager {
	/*@Autowired
	public PoolCacheManager(@Qualifier("memcachedClient")MemcachedClient client){
		this.client = client;
	}
	*//**
	 * 获取池中ip集合
	 * @throws CacheNotSupportException 
	 *//*
	public Set getPoolIpSet(String poolKey) throws CacheNotSupportException{
		Set ipSet = (Set)this.get(poolKey);
		return ipSet;
	}
	
	public void add(ProxyIpBean proxyIp,String poolCode) throws CacheNotSupportException{
		Set ipSet = this.getPoolIpSet(poolCode);
		if(ipSet==null){
			throw new CacheNotSupportException("不存在poolCode");
		}
		String realKey = this.getRealKey(proxyIp);
		if(ipSet.contains(realKey)){
			Log.info("ip 已存在于 "+poolCode);
		}else{
			ipSet.add(realKey);
		}
	}
	public void addIpChannel(ProxyIpBean proxyIp ,String poolCode)throws CacheNotSupportException{
		Set<String> channelSet = this.getPoolIpSet(poolCode);
		if(channelSet == null){
			channelSet = new HashSet<String>();
		}
	}

*//**
 *  数据库池增加代理ip
 * @param proxyIp
 * @throws CacheNotSupportException 
 *//*
	public void addIpDb(ProxyIpBean proxyIp) throws CacheNotSupportException{
		this.addPoolAndCache(proxyIp, DbPool.POOL_CODE);
	}
	*//**
	 * 新增到公共池
	 * @param proxyIp
	 * @throws CacheNotSupportException
	 *//*
	public void addIpPool(ProxyIpBean proxyIp) throws CacheNotSupportException{
		this.addPoolAndCache(proxyIp, PublicPool.POOL_CODE);
	}
	
*//**
 * ip新增于池同时增加到memcached
 * @param proxyIp
 * @param poolCode
 * @return
 * @throws CacheNotSupportException 
 *//*
	private void addPoolAndCache(ProxyIpBean proxyIp ,String poolCode) throws CacheNotSupportException{
		Set<String> dbSet = this.getPoolIpSet(poolCode);
		if(dbSet == null){
			dbSet = new HashSet<String>();
		}
		String realKey = this.getRealKey(proxyIp);
		if(this.contains(dbSet,proxyIp)){
			Log.info("ip 已存在于"+poolCode+"池中");
		}else{
			dbSet.add(realKey);
		}
		
		*//**下面开始新增**//*
		if(!this.contains(proxyIp)){
			this.add(proxyIp);
		}
		if(dbSet == null){
			this.put(poolCode, dbSet);
		}else{
			this.update(poolCode, dbSet);
		}
	}
	
	
	
	private String getIp(ProxyIpBean proxyIp) throws CacheNotSupportException{
		if(proxyIp==null){
			throw new CacheNotSupportException("proxyIp 不能为空");
		}
		if(StringUtils.isBlank(proxyIp.getIp())){
			throw new CacheNotSupportException("proxyIp.ip 不能为空");
		}
		return proxyIp.getIp();
	}
	
*//**
 * 获取代理ip真实的键	
 * @param proxyIp
 * @return
 * @throws CacheNotSupportException
 *//*
	private String getRealKey(ProxyIpBean proxyIp) throws CacheNotSupportException{
		if(proxyIp==null){
			throw new CacheNotSupportException("proxyIp 不能为空");
		}
		if(StringUtils.isBlank(proxyIp.getIp())){
			throw new CacheNotSupportException("proxyIp.ip 不能为空");
		}
		return new StringBuffer(this.DEFALUT_CACHE_KEY_PRE).append(proxyIp.getIp()).toString();
	}
	*//**
	 * 判断池中是否存在该代理ip
	 * @param poolSet
	 * @param proxyIp
	 * @return
	 * @throws CacheNotSupportException
	 *//*
	private boolean contains(Set poolSet,ProxyIpBean proxyIp) throws CacheNotSupportException{
		return poolSet.contains(this.getRealKey(proxyIp));
	}
*//**
 * 判断memcached是否存在代理ip	
 * @param proxyIp
 * @return
 * @throws CacheNotSupportException
 *//*
	private boolean contains(ProxyIpBean proxyIp) throws CacheNotSupportException{
		return this.get(proxyIp.getIp())!=null;
	}
*//**
 * 往memcached 添加代理ip	
 * @param proxyIp
 * @throws CacheNotSupportException
 *//*
	private void add(ProxyIpBean proxyIp) throws CacheNotSupportException{
		this.put(proxyIp.getIp(), proxyIp);
	}*/
	

}
